package com.cml.dm.admin.service.sql;

import com.cml.dm.admin.entity.DmExeLogger;
import com.cml.dm.admin.entity.DmMetadata;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JoinNodeBackupSqlGenerateService extends DefaultBackupSqlGenerateService {
    @Override
    public List<String> doSqlGenerate(DmMetadata dmMetadata, DmExeLogger dmExeLogger, Object extParameter) {
        List<String> sqlList = new ArrayList<>();
        String sql = dmMetadata.getBackupSql() + " where " +dmMetadata.getJoinUpPkNode() +" in ";
        DmMetadata upMetadata =dmMetadata.getUpDmMetadata();
        DmMetadata joinNode = dmMetadata.getJoinNode();
        if(null == joinNode){
            joinNode = upMetadata;
        }
        String upNodeSql = super.doSqlGenerate(joinNode, dmExeLogger, extParameter).get(0);
        //根据join字段替换生成sql语句
        upNodeSql =  "select " + joinNode.getPk()  +upNodeSql.substring(upNodeSql.indexOf(" from"),upNodeSql.length());
        sqlList.add(sql + "(" + upNodeSql +")");
        return sqlList;
    }
}
